
package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.BoxOutStockAdapter;
import com.mflex.WHSmartWhouseAd.adapter.BoxOutStockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.adapter.MaterialOutStockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.model.MaterialDetailForRawModel;
import com.mflex.WHSmartWhouseAd.model.RequestModel;
import com.mflex.WHSmartWhouseAd.model.ScanDataForRawBoxModel;
import com.mflex.WHSmartWhouseAd.model.ScanDataForRawModel;
import com.mflex.WHSmartWhouseAd.model.ScanVehPlateForRawModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailForRawBoxModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailSubimtForRawModel;
import com.mflex.WHSmartWhouseAd.model.TaskForRawBoxModel;
import com.mflex.WHSmartWhouseAd.model.TaskIssueForRawBoxModel;
import com.mflex.WHSmartWhouseAd.model.TaskSubimtForRawBoxModel;
import com.mflex.WHSmartWhouseAd.model.WHCompCheckTaskDetails;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.reflect.TypeToken;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;


public class BoxOutStockTaskDetailActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText txtTaskNo;

    EditText txtArea;

    EditText txtUnremovedQty;
    EditText txtRemovedQty;
    EditText txtCarNo;
    EditText txtBoxNo;
    EditText txtEmployee;

    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;

    Button buttonClear;
    Button buttonSubmit;
    String TaskDetailID;
    String userName;
    String OPT;
    private double scanQty;


    List<TaskIssueForRawBoxModel> TaskIssueListForRawBox;
    TaskIssueForRawBoxModel taskIssueForRawBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_box_outstock_task_detail);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        txtList = findViewById(R.id.lvList);

        txtTaskNo = findViewById(R.id.stvTaskNO);

        txtArea = findViewById(R.id.stvArea);

        txtUnremovedQty = findViewById(R.id.stvUnremovedQty);
        txtRemovedQty = findViewById(R.id.stvRemovedQty);
        txtCarNo = findViewById(R.id.stvCarNo);
        txtBoxNo = findViewById(R.id.stvBoxNo);
        txtEmployee= findViewById(R.id.stvEmployee);

        buttonClear = findViewById(R.id.button_clear);
        buttonSubmit = findViewById(R.id.button_save);

        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        OPT = intent.getStringExtra("OPT");

        txtTaskNo.setText(intent.getStringExtra("TaskNo"));
        XHttp.get("api/WHSmartWhousePDA/GetSmartTaskDetailListForRawBox")
                .params("TaskNo", txtTaskNo.getText().toString())
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<TaskDetailForRawBoxModel>() {
                    @Override
                    public void onSuccess(TaskDetailForRawBoxModel response) {
                        mMiniLoadingDialog.dismiss();
                        if(response==null)
                        {
                            mMiniLoadingDialog.dismiss();
                            MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);mp.start();
                            XToastUtils.success("GetSmartTaskDetailListForRawBox:返回的Data=null", 1500);
                            txtEmployee.setText("");
                            txtEmployee.requestFocus();
                            return ;
                        }
                        TaskIssueListForRawBox=response.getTaskIssueListForRawBox();
                        txtArea.setText(response.getOriginalArea());
                        txtUnremovedQty.setText(response.getUnremovedQty());
                        txtRemovedQty.setText(response.getRemovedQty());
                        BoxOutStockTaskDetailAdapter BoxOutStockTaskDetailAdapter = new BoxOutStockTaskDetailAdapter(BoxOutStockTaskDetailActivity.this, TaskIssueListForRawBox);
                        txtList.setAdapter(BoxOutStockTaskDetailAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        txtEmployee.setText("");
                        txtEmployee.requestFocus();
                        mMiniLoadingDialog.dismiss();
                        new MaterialDialog.Builder(BoxOutStockTaskDetailActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(e.getMessage())
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });

        txtEmployee.requestFocus();

        buttonClear.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                   Intent intent=new Intent(BoxOutStockTaskDetailActivity.this,MaterialOutStockTaskActivity.class);
                                intent.putExtra("TaskNo", txtTaskNo.getText().toString());
                                startActivity(intent);
            }
        });

        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//                mMiniLoadingDialog.show();
                TaskSubimtForRawBoxModel model=new TaskSubimtForRawBoxModel(txtCarNo.getText().toString(),txtTaskNo.getText().toString(),userName);
                RequestModel requestModel = new RequestModel();
                requestModel.setJson(JsonUtil.toJson(model));
                String json = JsonUtil.toJson(requestModel);
                System.out.println(json);
                XHttp.post("api/WHSmartWhousePDA/TaskSubmitForRawBox")
                        .upJson(JsonUtil.toJson(requestModel))
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(150000)
                        .execute(new SimpleCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean response) {
                                mMiniLoadingDialog.dismiss();
                                XToastUtils.success("提交成功",13000);
//                                Intent intent=new Intent(MaterialOutStockTaskDetailActivity.this,MaterialOutStockTaskActivity.class);
//                                intent.putExtra("TaskNo", txtTaskNo.getText().toString());
//                                startActivity(intent);
                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(BoxOutStockTaskDetailActivity.this)
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });

            }
        });

        txtEmployee.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",txtEmployee.getText().toString()))  {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        XToastUtils.success("请扫描操作人号", 1500);
                        txtCarNo.requestFocus();
                        return false;
                    }

                    XHttp.get("api/WHSmartWhousePDA/CheckOperatorForRaw")
                            .params("Operator", txtEmployee.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<Boolean>() {
                                @Override
                                public void onSuccess(Boolean response) {
                                    txtBoxNo.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    txtEmployee.setText("");
                                    txtEmployee.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                                    mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        txtCarNo.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",txtCarNo.getText().toString()))  {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        XToastUtils.success("请先扫描车号", 1500);
                        txtCarNo.requestFocus();
                        return false;
                    }

                    XHttp.get("api/WHSmartWhousePDA/CheckCarNoForRaw")
                            .params("CarNo", txtCarNo.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<Boolean>() {
                                @Override
                                public void onSuccess(Boolean response) {
                                    txtBoxNo.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    txtCarNo.setText("");
                                    txtCarNo.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                                    mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        txtBoxNo.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
//                mMiniLoadingDialog.show();
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if(StringUtils.equals("",txtBoxNo.getText().toString())) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        XToastUtils.success("请先扫描箱号", 1500);
                        txtBoxNo.requestFocus();
                        return false;
                    }
                    if(StringUtils.equals("",txtEmployee.getText().toString()))  {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                        mp.start();
                        XToastUtils.success("请扫描操作人号", 1500);
                        txtCarNo.requestFocus();
                        return false;
                    }
                    mMiniLoadingDialog.show();
                    ScanDataForRawBoxModel model = new ScanDataForRawBoxModel(txtCarNo.getText().toString(), txtEmployee.getText().toString(), txtBoxNo.getText().toString(),txtTaskNo.getText().toString());
                    RequestModel requestModel = new RequestModel();
                    requestModel.setJson(JsonUtil.toJson(model));
                    String json = JsonUtil.toJson(requestModel);
                    System.out.println(json);
                    XHttp.post("api/WHSmartWhousePDA/ScanDataSubmitForRawBox")
                            .upJson(JsonUtil.toJson(requestModel))
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<TaskIssueForRawBoxModel>() {
                                @Override
                                public void onSuccess(TaskIssueForRawBoxModel response) {

                                    mMiniLoadingDialog.dismiss();
                                    taskIssueForRawBox = response;

                                    scanQty = scanQty + Double.parseDouble(response.getQty());
                                    txtRemovedQty.setText(String.valueOf(scanQty));


                                    Boolean flag = false;
                                    for (TaskIssueForRawBoxModel detail : TaskIssueListForRawBox) {
                                        if (StringUtils.equals(taskIssueForRawBox.getVehicleNumber(), detail.getVehicleNumber())) {
                                            flag = true;
                                            detail.setFlag("2");
//                                            int scannum = Integer.parseInt(detail.getScannum()) + 1;
//                                            detail.setScannum(String.valueOf(scannum));
                                            break;
                                        }

                                        int qty=(int)Math.round(Double.parseDouble(txtRemovedQty.getText().toString()));
                                        if(StringUtils.equals(String.valueOf(qty), txtUnremovedQty.getText().toString()))
                                        {
                                            detail.setFlag("2");//都下完是绿色
                                        }
                                    }



                                    if (!flag) {
                                        new MaterialDialog.Builder(BoxOutStockTaskDetailActivity.this)
                                                .iconRes(R.drawable.icon_warning)
                                                .title(R.string.tip_warning)
                                                .content("载具号/盘号不在列表中")
                                                .positiveText(R.string.lab_submit)
                                                .show();

                                        return;
                                    }

                                    Collections.sort(TaskIssueListForRawBox, new Comparator<TaskIssueForRawBoxModel>() {
                                        @Override
                                        public int compare(TaskIssueForRawBoxModel t0, TaskIssueForRawBoxModel t1) {
                                            if (!StringUtils.equals("2", t0.getFlag()) && StringUtils.equals("2", t1.getFlag())) {
                                                return -1;
                                            }
                                            return 0;

                                        }
                                    });

                                    BoxOutStockTaskDetailAdapter BoxOutStockTaskDetailAdapter = new BoxOutStockTaskDetailAdapter(BoxOutStockTaskDetailActivity.this, TaskIssueListForRawBox);
                                    txtList.setAdapter(BoxOutStockTaskDetailAdapter);
                                    txtBoxNo.setText("");
                                    txtBoxNo.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    txtBoxNo.setText("");
                                    txtBoxNo.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(BoxOutStockTaskDetailActivity.this, R.raw.error_voice);
                                    mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });


    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}



